package com.lm.temporal.controller;

import com.lm.temporal.workflow.OrderWorkflow;
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowOptions;
import io.temporal.worker.WorkerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private WorkflowClient workflowClient;

    @Autowired
    private WorkerFactory factory;

    @PostMapping("/{orderId}")
    public String process(@PathVariable String orderId) {
        OrderWorkflow workflow = workflowClient.newWorkflowStub(
                OrderWorkflow.class,
                WorkflowOptions.newBuilder()
                        .setTaskQueue("ORDER_TASK_QUEUE")
                        .build()
        );

        WorkflowClient.start(workflow::processOrder, orderId);
        factory.start();
        return "✅ 订单已提交处理: " + orderId;
    }
}
